﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using XSC.Module.Mysql;
using XSC.Runtime;
using Dapper;
using System.Linq;

namespace TestConsoleApp1
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string bms_bill = "Server=localhost;Port=61845;Initial Catalog=bms_system;Uid=root;Pwd=!(/Qqi+9ka0y;Allow User Variables=true";
            string bms = "Server=localhost;Port=61845;Initial Catalog=bms_bill;Uid=root;Pwd=!(/Qqi+9ka0y;Allow User Variables=true";
            var con = new MySqlConnector.MySqlConnection(bms_bill);
            var bms_con = new MySqlConnector.MySqlConnection(bms);
            var sql = "select Id, request from open_api_callback_log where Id > @id order by id limit 100";
            long id = 44401;
            var updatesql = "update open_api_callback_log set PrjId='{0}', BizNo='{1}', Code='{2}' where Id='{3}';";
            var updateSync = "update standingbook_sync set timestamp=timestamp, BizNo='{0}',Code='{1}' where StandingbookId='{2}';";
            while (true)
            {
                var datas = await con.QueryAsync<ApiLog>(sql, new { id });
                if (datas?.Any() != true)
                {
                    break;
                }
                StringBuilder sb = new StringBuilder();
                StringBuilder sb1 = new StringBuilder();
                foreach (var item in datas)
                {
                    if(item.Id > id)
                    {
                        id = item.Id;
                    }
                    var rq = Newtonsoft.Json.JsonConvert.DeserializeObject<StandingbookBillDetailPushRequest>(item.request);
                    sb.AppendFormat(updatesql, rq.ProjectId, rq.GetBizNo(), rq.GetCode(), item.Id);
                    var bizNo = $"{rq.ProjectId}|{rq.Standingbooks.FirstOrDefault().BusinessTypeCode}|{rq.Standingbooks.FirstOrDefault().BusinessNumber}".ToSHA1();
                    sb1.AppendFormat(updateSync, bizNo, rq.GetCode(), rq.Standingbooks.FirstOrDefault().Id);
                }
                try
                {
                    await con.ExecuteAsync(sb.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                try
                {
                    await bms_con.ExecuteAsync(sb1.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
              
                Console.WriteLine(id);
            }
            Console.WriteLine("push end");
            

        }
        public static string SHA1(string code)
        {
            return BitConverter.ToString(new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(code))).ToUpper().Replace("-", "");
        }
    }
    public class ApiLog
    {
        public long Id { get; set; }
        public string request { get; set; }
    }
}
